package com.atguigu.ssyx.home.service.impl;

import com.atguigu.ssyx.activity.client.ActivityFeignCilent;
import com.atguigu.ssyx.client.prouduct.ProductFeignClient;
import com.atguigu.ssyx.client.search.SkuFeignClient;
import com.atguigu.ssyx.home.service.ItemService;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ExecutorService executorService;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ActivityFeignCilent activityFeignClient;

    @Autowired
    private SkuFeignClient skuFeignClient;
    /**
     *
     * @param id  商品id
     * @param userId 用户id
     * @return
     */
    @Override
    public Map<String, Object> item(Long id, Long userId) {
        Map<String, Object> skuMap = new HashMap<String, Object>();
//找到对应的商品详情。
        CompletableFuture<SkuInfoVo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoVo skuInfo = productFeignClient.getSkuInfoVo(id);// TODO
            skuMap.put("skuInfoVo", skuInfo);
            return skuInfo;
        }, executorService);


        CompletableFuture<Void> ActiveCompletableFuture = CompletableFuture.runAsync(() -> {
//            优惠券信息长什么样子。
//            调用优惠券。
            Map<String, Object> activeMap = activityFeignClient.findActivityAndCoupon(id, userId);//ToDo
            skuMap.putAll(activeMap);
        }, executorService);

//        更新热度es中的热度信息.
        CompletableFuture<Void> HotCompletableFuture = CompletableFuture.runAsync(() -> {
            //远程调用serarch
            Boolean aBoolean = skuFeignClient.updateHots(id);
        }, executorService);

        CompletableFuture.allOf(skuInfoCompletableFuture, ActiveCompletableFuture,HotCompletableFuture).join();
        return skuMap;
    }
}
